import { Component, OnInit } from '@angular/core';
import { Teacher } from '../teacher';
import { CourseService } from '../../service/course.service';
import { Router } from '@angular/router';

@Component({
  selector: 'app-teacher',
  templateUrl: './teacher.component.html',
  styleUrls: ['./teacher.component.css']
})
export class TeacherComponent implements OnInit {
  r:Teacher;
  rl:Array<Teacher>
  constructor(
    private courseservice:CourseService,
    private router:Router) {
      this.r=new Teacher();
     }

  ngOnInit() {
    this.loadTeacherList();
  }

  addTeacher(){
    this.r=new Teacher();
  }

  save(){
    if(this.r.id){
      this.update();
    }
    else{
      this.add();
    }
  }

  goBack(){
    this.router.navigate(['courseindex']);
  }

  add(){
    this.courseservice.addTeacher(this.r)
    .then((flag:boolean)=>{
    if(flag== true){
      this.r =new Teacher();
      this.loadTeacherList()
    }
    else{
      alert("添加异常，请重试");
    }
    })
  }

  update(){
    this.courseservice.updateTeacher(this.r)
    .then((flag:boolean)=>{
     if(flag == true){
       this.r= new Teacher();
       this.loadTeacherList();
     }
     else{
       alert("修改异常，请重试");
     }
    })
  }

  toupdate(r:Teacher){
    this.r=r;
  }

  deleteTeacher(r:Teacher){
    this.courseservice.deleteTeacher(r)
    .then((flag:boolean)=>{
     if(flag == true){
       this.loadTeacherList();
     }
     else{
       alert("删除异常，请重试");
     }
    })
  }
  
  loadTeacherList(){
    this.rl=new Array();
    this.courseservice.getTeacherList().then((data:any)=>{
      this.rl = data;
    })
  }


}
